#include "World.h"
#include "Camera.h"

World::World() {
	//Init nodes
	for (int i = 0; i < sizeX; i++){
		for (int j = 0; j < sizeY; j++){
			for (int k = 0; k < sizeZ; k++){
				nodes[i][j][k] = new Node(Vector3f(i, j, k), 1);
			}
		}
	}
	
	for (int i = 0; i < sizeX; i++){
		for (int j = 0; j < sizeY; j++){
			for (int k = 0; k < sizeZ; k++){
				if (i > 0){
					Structural* stSpring = new Structural(RESTLENGTH, STIFFNESS, DAMPING, nodes[i][j][k], nodes[i-1][j][k]);
					nodes[i][j][k]->AddStructural(stSpring);
					nodes[i-1][j][k]->AddStructural(stSpring);
				}
				if (j > 0){
					Structural* stSpring = new Structural(RESTLENGTH, STIFFNESS, DAMPING, nodes[i][j][k], nodes[i][j-1][k]);
					nodes[i][j][k]->AddStructural(stSpring);
					nodes[i][j-1][k]->AddStructural(stSpring);
				}
				if (k > 0){
					Structural* stSpring = new Structural(RESTLENGTH, STIFFNESS, DAMPING, nodes[i][j][k], nodes[i][j][k-1]);
					nodes[i][j][k]->AddStructural(stSpring);
					nodes[i][j][k-1]->AddStructural(stSpring);
				}
			}
		}
	}
}

World::~World() {

}

GLvoid World::Initialize() {
	prevTime = clock();
}

GLvoid World::Draw() {
	clock_t currentTime = clock();
	timeSinceLastFrame =currentTime - prevTime;
	prevTime = currentTime;

	Camera::TransformCamera();
	glBegin(GL_POINTS);

	for (int i = 0; i < sizeX; i++){
		for (int j = 0; j < sizeY; j++){
			for (int k = 0; k < sizeZ; k++){
				Node* node = nodes[i][j][k];
				node->Update(timeSinceLastFrame);

				Vector3f pos = node->getPosition();
				glColor3f(10, 222, 3);
				glVertex3f(pos.x, pos.y, pos.z);
			}
		}
	}

	glEnd();
}

float World::FrameTime() {
	return (float)(timeSinceLastFrame / CLOCKS_PER_SEC);
}